package com.ruoyi.system.utils;


import com.ruoyi.system.domain.AssessType;


import com.ruoyi.system.domain.dto.EmployeeAssessDetailVO.Indicators;
import com.ruoyi.system.domain.dto.EmployeeAssessDetailVO.IndicatorsType;
import com.ruoyi.system.mapper.AssessTypeMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class IndicatorsClassifier {
    // 私有构造函数，防止实例化
    private IndicatorsClassifier() {
        throw new UnsupportedOperationException("Utility class cannot be instantiated");
    }

    /**
     * 根据 indicatorsTypeId 分类 Indicators，生成 List<IndicatorsType>
     * @param indicators 输入的 Indicators 列表
     * @param assessTypeMapper 用于查询 AssessType 的 Mapper
     * @return List<IndicatorsType> 分类后的结果
     */
    public static List<IndicatorsType> classifyIndicators(List<Indicators> indicators, AssessTypeMapper assessTypeMapper) {
        // 处理空输入
        if (indicators == null || indicators.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取所有 AssessType 列表
        List<AssessType> assessTypes = assessTypeMapper.selectAssessTypeList();
        // 构建 typeName 映射
        Map<Integer, String> typeNameMap = assessTypes.stream()
                .filter(type -> type.getDelFlag() == 0) // 过滤 delFlag = 0
                .collect(Collectors.toMap(
                        AssessType::getId,
                        AssessType::getType,
                        (existing, replacement) -> existing // 解决 ID 冲突
                ));

        // 按 indicatorsTypeId 分组
        Map<Integer, List<Indicators>> groupedIndicators = indicators.stream()
                .collect(Collectors.groupingBy(Indicators::getIndicatorsTypeId));

        // 创建 List<IndicatorsType>
        List<IndicatorsType> result = new ArrayList<>();
        for (Map.Entry<Integer, List<Indicators>> entry : groupedIndicators.entrySet()) {
            Integer typeId = entry.getKey();
            List<Indicators> indicatorsList = entry.getValue();

            IndicatorsType indicatorsType = new IndicatorsType();
            indicatorsType.setIndicatorsTypeId(typeId);
            // 设置 typeName，从映射获取或使用默认值
            indicatorsType.setTypeName(typeNameMap.getOrDefault(typeId, "Type_" + typeId));
            indicatorsType.setIndicators(indicatorsList);

            result.add(indicatorsType);
        }

        return result;
    }
}